package org.kabeja.parser.table;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.entity.DXFLineType;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/25 14:15
 */
public class DXFLineTypeTableHandler extends AbstractTableHandler {
    public static final int GROUPCODE_LTYPE_NAME = 2;
    public static final int GROUPCODE_LTYPE_DESCRIPTION = 3;
    public static final int GROUPCODE_LTYPE_ALIGNMENT = 72;
    public static final int GROUPCODE_LTYPE_SEGMENT = 49;
    public static final int GROUPCODE_LTYPE_LENGTH = 40;
    public static final int GROUPCODE_LTYPE_SEGMENT_COUNT = 73;
    public static final int GROUPCODE_LTYPE_SCALE = 46;
    private DXFLineType ltype;
    private int segmentCount = 0;
    private double[] pattern;

    public DXFLineTypeTableHandler() {
    }

    @Override
    public void endParsing() {
        this.ltype.setPattern(this.pattern);
        this.doc.addDXFLineType(this.ltype);
    }

    @Override
    public String getTableKey() {
        return DXFConstants.TABLE_KEY_LTYPE;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch(var1) {
            case GROUPCODE_LTYPE_NAME:
                this.ltype.setName(var2.getValue());
                break;
            case GROUPCODE_LTYPE_DESCRIPTION:
                this.ltype.setDescritpion(var2.getValue());
                break;
            case GROUPCODE_LTYPE_LENGTH:
                this.ltype.setPatternLength(var2.getDoubleValue());
                break;
            case GROUPCODE_LTYPE_SCALE:
                this.ltype.setScale(var2.getDoubleValue());
                break;
            case GROUPCODE_LTYPE_SEGMENT:
                this.pattern[this.segmentCount] = var2.getDoubleValue();
                ++this.segmentCount;
                break;
            case GROUPCODE_LTYPE_ALIGNMENT:
                this.ltype.setAlignment(var2.getIntegerValue());
                break;
            case GROUPCODE_LTYPE_SEGMENT_COUNT:
                int var3 = var2.getIntegerValue();
                this.pattern = new double[var3];
                this.segmentCount = 0;
                break;
            default:
                break;
        }

    }

    @Override
    public void startParsing() {
        this.ltype = new DXFLineType();
        this.segmentCount = 0;
        this.pattern = null;
    }
}
